/*
 * Copyright 2009 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.splink.deepsplink.page {
	import org.splink.deepsplink.navigation.command.INavigationCommand;
	import org.splink.deepsplink.page.state.PageState;
	import org.splink.library.queue.IQable;
	import org.splink.library.tree.Tree;

	import flash.display.DisplayObjectContainer;

	/**
	 * Each page node within the configuration xml is represented by
	 * a <code>Page</code> instance. Each <code>Page</code> instance
	 * is created when the <code>Bootstrapper</code> is started. 
	 * 
	 * When a page is requested, the following happens:
	 * 1) <code>initialize</code> is executed
	 * 2) When initialization is done, the page must invoke <code>setInitialized</code>
	 * 3) the show strategy is executed
	 * 
	 * When a page is left, the following happens:
	 * 1) the hide strategy is executed
	 * 2) <code>finalize</code> is executed
	 * 
	 * When the parameters change, the <code>onParamsChanged</code> method of 
	 * all pages which are currently in the <code>PageState.SHOWN_STATE</code>
	 * is executed.
	 * 
	 * @author Max Kugland
	 */
	public class Page implements IPage, IPageSupplier {

		private var _supplier : IPageSupplier;

		public function Page(supplier : IPageSupplier) {
			_supplier = supplier;
		}

		/**
		 * @inheritDoc
		 */
		public function onParamsChanged(params : Array) : void {
		}

		/**
		 * @inheritDoc
		 */
		public function initialize() : void {
			setInitialized();
		}

		/**
		 * @inheritDoc
		 */
		final public function setInitialized() : void {
			_supplier.setInitialized();
		}
		
		/**
		 * @inheritDoc
		 * 
		 * Finalizes the page. Note that the page is only instantiated once and the 
		 * page may be initialized again. 
		 * After <code>finalize</code> has been executed, it's 
		 * <code>DisplayObjectContainer</code> will be removed from the displaylist.
		 */
		public function finalize() : void {
		}

		/**
		 * @inheritDoc
		 */
		final public function get id() : String {
			return _supplier.id;
		}

		/**
		 * @inheritDoc
		 */
		final public function setShowStrategy(showStrategy : IQable) : IQable {
			return _supplier.setShowStrategy(showStrategy);
		}

		/**
		 * @inheritDoc
		 */
		final public function setHideStrategy(hideStrategy : IQable) : IQable {
			return _supplier.setHideStrategy(hideStrategy);
		}

		/**
		 * @inheritDoc
		 */
		final public function get title() : String {
			return _supplier.title;
		}

		/**
		 * @inheritDoc
		 */
		final public function getParamValueForKey(key : String) : String {
			return _supplier.getParamValueForKey(key);
		}

		/**
		 * @inheritDoc
		 */
		final public function get state() : PageState {
			return _supplier.state;
		}

		/**
		 * @inheritDoc
		 */
		final public function get navigationCommand() : INavigationCommand {
			return _supplier.navigationCommand;
		}

		/**
		 * @inheritDoc
		 */
		final public function get params() : Array {
			return _supplier.params;
		}

		/**
		 * @inheritDoc
		 */
		final public function get tree() : Tree {
			return _supplier.tree;
		}

		/**
		 * @inheritDoc
		 */
		final public function get display() : DisplayObjectContainer {
			return _supplier.display;
		}
	}
}
